package com.baoli.blzj.customviews;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baoli.blzj.R;

/**
 * 自定义导航栏 by John
 */

public class CustomTitleBar {
    private View titleView;
    private LinearLayout titleBar;
    private TextView text;
    private LinearLayout left_layout;
    private RelativeLayout right_layout;
    private TextView rightText;
    private ImageView rightIco,leftIco;

    /**
     * 构造方法：用于获取对象
     */
    public CustomTitleBar(Activity context) {
        titleView = context.findViewById(R.id.title_bar);
        text = (TextView) titleView.findViewById(R.id.title_text);
        titleBar = (LinearLayout) titleView.findViewById(R.id.title_bar);
        left_layout = (LinearLayout) titleView.findViewById(R.id.back_layout);
        right_layout = (RelativeLayout) titleView.findViewById(R.id.right_layout);
        leftIco = (ImageView) titleView.findViewById(R.id.title_leftIcon);
        rightText = (TextView) titleView.findViewById(R.id.title_rightText);
        rightIco = (ImageView) titleView.findViewById(R.id.title_rightIcon);
    }

    /**
     * 用于设置标题栏文字
     */
    public CustomTitleBar setTitleText(String titleText) {
        if (!TextUtils.isEmpty(titleText)) {
            text.setText(titleText);
        }
        return this;
    }

    /**
     * 用于设置标题栏左边要显示的图片
     */
    public CustomTitleBar setLeftIco(int resId) {
        leftIco.setVisibility(View.VISIBLE);
        leftIco.setBackgroundResource(resId);
        return this;
    }

    /**
     * 用于设置标题栏右边要显示的图片
     */
    public CustomTitleBar setRightIco(int resId) {
        rightText.setVisibility(View.INVISIBLE);
        rightIco.setVisibility(View.VISIBLE);
        rightIco.setBackgroundResource(resId);
        return this;
    }
    /**
     * 用于设置标题栏右边要显示的文字
     */
    public CustomTitleBar setRightText(String titleText) {
        if (!TextUtils.isEmpty(titleText)) {
            rightIco.setVisibility(View.INVISIBLE);
            rightText.setVisibility(View.VISIBLE);
            rightText.setText(titleText);
        }
        return this;
    }

    /**
     * @param isShow 关闭返回键
     * @return
     */
    public CustomTitleBar setIsShowLeftLayout(boolean isShow){
        if (!isShow){
            left_layout.setVisibility(View.INVISIBLE);
        }
        return this;
    }

    /**
     * 用于设置标题栏左边的单击事件
     */
    public CustomTitleBar setLeftIcoListening(OnClickListener listener) {
        if (left_layout.getVisibility() == View.VISIBLE) {
            left_layout.setOnClickListener(listener);
        }
        return this;
    }

    /**
     * 用于设置标题栏右边的单击事件
     */
    public CustomTitleBar setRightIcoListening(OnClickListener listener) {
        if (right_layout.getVisibility() == View.VISIBLE) {
            right_layout.setOnClickListener(listener);
        }
        return this;
    }
} 